﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
using System.Data;

namespace PMS.Web
{
    public partial class FinanceState : ExportFiles
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string role = PageComm.GetUser(this.Response).role;
                if (role != "管理员")
                {
                    btn_Add.Visible = false;
                    btn_AddFinanceState.Visible = false;
                    gv_PMS_ProjectFinance.Columns[gv_PMS_ProjectFinance.Columns.Count - 1].Visible = false;
                    gv_PMS_ProjectFinance.Columns[gv_PMS_ProjectFinance.Columns.Count - 2].Visible = false;
                }
                string projectID = Request["projectID"];
                if (!string.IsNullOrEmpty(projectID))
                {
                    hid_ProjectID.Value = projectID;
                    ProjectLoad();
                    DataLoad();
                }
            }
        }

        /// <summary>
        /// 加载项目信息
        /// </summary>
        private void ProjectLoad()
        {
            string projectID = hid_ProjectID.Value;
            PMS_ProjectBLL pbll = new PMS_ProjectBLL();
            PMS_Project project = pbll.GetModel(Convert.ToInt32(projectID));
            lab_ProjectID.Text = project.projectID;
            lab_JDEID.Text = project.JDEID;
            lab_Customer.Text = project.customer;
            lab_CustomerTel.Text = project.customerTel;
            lab_ProjectName.Text = project.projectName;
            lab_ProjectType.Text = project.projectType;
            lab_SystemPath.Text = project.systemPath;
            lab_City.Text = project.city;
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private PMS_ProjectFinanceQuery GetQuery()
        {
            PMS_ProjectFinanceQuery query = new PMS_ProjectFinanceQuery();
            query.projectID = Convert.ToInt32(hid_ProjectID.Value);
            if (!string.IsNullOrEmpty(txt_FromDate.Text.Trim()))
                query.startDate = Convert.ToDateTime(txt_FromDate.Text.Trim());
            if (!string.IsNullOrEmpty(txt_ToDate.Text.Trim()))
                query.endDate = Convert.ToDateTime(txt_ToDate.Text.Trim());
            return query;
        }

        /// <summary>
        /// 列表绑定
        /// </summary>
        /// <param name="projectID"></param>
        private void DataLoad()
        {
            try
            {
                PMS_ProjectFinanceQuery query = GetQuery();
                PMS_ProjectFinanceBLL bll = new PMS_ProjectFinanceBLL();
                List<PMS_ProjectFinance> liste = bll.GetList(query);
                this.gv_PMS_ProjectFinance.DataSource = liste;
                this.gv_PMS_ProjectFinance.DataBind();
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 加载当前记录
        /// </summary>
        /// <param name="id"></param>
        public void LoadEditText(int id)
        {
            PMS_ProjectFinanceBLL bll = new PMS_ProjectFinanceBLL();
            PMS_ProjectFinance model = bll.GetModel(id);
            hid_ID.Value = model.id.ToString();
            txt_ApplyAmount.Text = model.applyAmount == null ? "" : model.applyAmount.Value.ToString("0.00");
            txt_ApplyStage.Text = model.applyStage;
            txt_ChangeVOAmount.Text = model.changeVOAmount == null ? "" : model.changeVOAmount.Value.ToString("0.00");
            txt_NoApplyAmount.Text = model.noApplyAmount == null ? "" : model.noApplyAmount.Value.ToString("0.00");
            txt_PayAmount.Text = model.payAmount == null ? "" : model.payAmount.Value.ToString("0.00");
            txt_Remark.Text = model.remark;
            txt_SettleAmount.Text = model.settleAmount == null ? "" : model.settleAmount.Value.ToString("0.00");
            txt_SigningAmount.Text = model.signingAmount == null ? "" : model.signingAmount.Value.ToString("0.00");
            txt_SigningDate.Text = model.signingDate == null ? "" : model.signingDate.Value.ToString("yyyy-MM-dd");
            this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>showContentDiv(1);</script>");
        }

        /// <summary>
        /// 清空
        /// </summary>
        public void ClearText()
        {
            hid_ID.Value = "0";
            txt_ApplyAmount.Text = "";
            txt_ApplyStage.Text = "";
            txt_ChangeVOAmount.Text = "";
            txt_NoApplyAmount.Text = "";
            txt_PayAmount.Text = "";
            txt_Remark.Text = "";
            txt_SettleAmount.Text = "";
            txt_SigningAmount.Text = "";
            txt_SigningDate.Text = "";
        }

        protected void gv_PMS_ProjectFinance_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edited")
            {
                if (e.CommandArgument != null)
                {
                    LoadEditText(Convert.ToInt32(e.CommandArgument));
                }
            }
            else if (e.CommandName == "Del")
            {
                if (e.CommandArgument != null)
                {
                    PMS_ProjectFinanceBLL bll = new PMS_ProjectFinanceBLL();
                    if (bll.Delete(Convert.ToInt32(e.CommandArgument)))
                    {
                        this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('删除成功');</script>");
                        DataBind();
                    }
                    else
                    {
                        this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('删除失败');</script>");
                    }
                }
            }
        }

        protected void gv_PMS_ProjectFinance_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            if (row.RowType == DataControlRowType.DataRow)
            {
                PMS_ProjectFinance model = row.DataItem as PMS_ProjectFinance;
                string repairsDate = "";
                if (model.signingDate != null && model.signingDate != DateTime.MinValue)
                {
                    repairsDate = model.signingDate.Value.ToString("yyyy-MM-dd");
                }
                row.Cells[4].Text = repairsDate;
            }
        }

        protected void btn_AddFinanceState_Click(object sender, EventArgs e)
        {
            PMS_ProjectFinanceBLL bll = new PMS_ProjectFinanceBLL();
            PMS_ProjectFinance model = new PMS_ProjectFinance();
            model.id = Convert.ToInt32(hid_ID.Value);
            model.projectID = hid_ProjectID.Value;
            model.customerName = lab_ProjectName.Text.Trim();
            try
            {
                if (!string.IsNullOrEmpty(txt_ApplyAmount.Text.Trim()))
                {
                    model.applyAmount = Convert.ToDecimal(txt_ApplyAmount.Text.Trim());
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('本月申请金额格式错误，请输入数字');</script>");
                return;
            }
            model.applyStage = txt_ApplyStage.Text.Trim();
            try
            {
                if (!string.IsNullOrEmpty(txt_ChangeVOAmount.Text.Trim()))
                {
                    model.changeVOAmount = Convert.ToDecimal(txt_ChangeVOAmount.Text.Trim());
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('变更 VO格式错误，请输入数字');</script>");
                return;
            }
            try
            {
                if (!string.IsNullOrEmpty(txt_NoApplyAmount.Text.Trim()))
                {
                    model.noApplyAmount = Convert.ToDecimal(txt_NoApplyAmount.Text.Trim());
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('未申请金额格式错误，请输入数字');</script>");
                return;
            }
            try
            {
                if (!string.IsNullOrEmpty(txt_PayAmount.Text.Trim()))
                {
                    model.payAmount = Convert.ToDecimal(txt_PayAmount.Text.Trim());
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('已支付金额格式错误，请输入数字');</script>");
                return;
            }
            model.remark = txt_Remark.Text.Trim();
            try
            {
                if (!string.IsNullOrEmpty(txt_SettleAmount.Text.Trim()))
                {
                    model.settleAmount = Convert.ToDecimal(txt_SettleAmount.Text.Trim());
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('最终结算金额格式错误，请输入数字');</script>");
                return;
            }
            try
            {
                if (!string.IsNullOrEmpty(txt_SigningAmount.Text.Trim()))
                {
                    model.signingAmount = Convert.ToDecimal(txt_SigningAmount.Text.Trim());
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('合同金额格式错误，请输入数字');</script>");
                return;
            }
            try
            {
                if (!string.IsNullOrEmpty(txt_SigningDate.Text.Trim()))
                {
                    model.signingDate = Convert.ToDateTime(txt_SigningDate.Text.Trim());
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('合同签订日期格式错误');</script>");
                return;
            }
            if (!bll.AddUpdate(model))
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('更新失败，请稍后重试');</script>");
                return;
            }
            else
            {
                ClearText();
                DataLoad();
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>showContentDiv(0);</script>");
            }
        }

        protected void btn_Search_Click(object sender, EventArgs e)
        {
            DataLoad();
        }

        protected void btn_Export_Click(object sender, EventArgs e)
        {
            PMS_ProjectFinanceQuery query = GetQuery();
            PMS_ProjectFinanceBLL bll = new PMS_ProjectFinanceBLL();
            DataTable dt = bll.GetListTable(query);
            if (dt != null && dt.Rows.Count > 0)
            {
                ExportExcel("工程项目财务", dt);
            }
        }
    }
}